//
//  SLSetBridgeWifiVC.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/19.
//

#import "SLSetBridgeWifiVC.h"
#import "SLConnectBridgeVC.h"
@interface SLSetBridgeWifiVC ()

@end

@implementation SLSetBridgeWifiVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubViews];
}

-(void)setUpSubViews{
    self.title = kLocalString(@"Add bridge");
    _nextBtn.btnStyle = 1;
    [_nextBtn setTitle:kLocalString(@"Next") forState:UIControlStateNormal];
    self.bgView1.layer.cornerRadius = 20;
    self.bgView1.layer.masksToBounds = YES;
    self.bgView2.layer.cornerRadius = 20;
    self.bgView2.layer.masksToBounds = YES;
    _tf1.text = @"HuoPro";
    _tf2.text = @"88889999";
//    _tf1.text = @"xiuxiu";
//    _tf2.text = @"xiuxiu888888";
    
    _topLbl.text = kLocalString(@"Apply the same Wi-fi setup on this phone.");
    _tfTip1.text = kLocalString(@"Wi-fi name");
    _tfTip2.text = kLocalString(@"Password");
    NSAttributedString *tipStr = [self highlightedAttributedStringWithOriginalString:kLocalString(@"Please make sure the Wi-fi is 2.4GHz, 5GHz Wi-fi is not supported.") highlightedString:kLocalString(@"2.4GHz")];
    _tipLbl3.attributedText = tipStr;
    [_checkBtn setLineTextWith:kLocalString(@"Find out whether my Wi-fi network is 2.4GHz, 5GHz or 2.4GHz & 5GHz dual band.")];
    _checkBtn.titleLabel.numberOfLines = 0;
    
    [_tf1 setPlaceholderWithPlaceholderText:kLocalString(@"Enter Wifi Name") withColor:KColor_placeholdar withFont:kFont(18)];
    [_tf2 setPlaceholderWithPlaceholderText:kLocalString(@"Enter Wifi Password") withColor:KColor_placeholdar withFont:kFont(18)];
}

- (NSAttributedString *)highlightedAttributedStringWithOriginalString:(NSString *)originalString highlightedString:(NSString *)highlightedString {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:originalString];
    
    // 找到需要高亮的子串在原字符串中的范围
    NSRange range = [originalString rangeOfString:highlightedString];
    
    // 如果找到了需要高亮的子串,则设置其颜色为黄色
    if (range.location != NSNotFound) {
        [attributedString addAttribute:NSForegroundColorAttributeName value:KColor_O range:range];
    }
    
    // 遍历原字符串中未高亮的部分,设置其颜色为黑色
    [attributedString enumerateAttributesInRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
        if (![attrs.allKeys containsObject:NSForegroundColorAttributeName]) {
            [attributedString addAttribute:NSForegroundColorAttributeName value:KColor_placeholdar range:range];
        }
    }];
    
    return [attributedString copy];
}

- (IBAction)checkAction:(id)sender {
    
}

- (IBAction)bextAction:(id)sender {
    if (_tf1.text.length > 20) {
        kShowToast(@"The wifi name cannot exceed 20 bytes");
        return;
    }
    if (_tf2.text.length < 8) {
        kShowToast(@"The wifi password must be at least 8 bytes");
        return;
    }
    if (_tf2.text.length > 20) {
        kShowToast(@"The wifi name cannot exceed 20 bytes");
        return;
    }
    
    //网关修改wifi
    if (_bridgeDetailVc) {
        SLConnectBridgeVC *vc = [[SLConnectBridgeVC alloc] init];
        vc.bridgeDetailVc = self->_bridgeDetailVc;
        vc.refreshBlock = self->_refreshBlock;
        vc.ssid = _tf1.text;
        vc.pswd = _tf2.text;
        [self.navigationController pushViewController:vc animated:YES];
    }
    //网关第一次配网
    else{
        [ZCTool showLoadingOnKeyWindow];
        [[SLBluetoothManager sharedManager] setGateWifiInfoWith:_tf1.text pswd:_tf2.text finish:^(NSString *commond, BOOL isSuccess, id data, NSString *errorMsg) {
            if (!isSuccess) {
                [ZCTool hideLoadingOnKeyWindow];
                kShowToast(@"Failed to set wifi.");
                return;
            }
            //设置成功，再设置MQTT主题
            [[SLBluetoothManager sharedManager] getGateMacAddressWith:^(NSString *commond, BOOL isSuccess, id data, NSString *errorMsg) {
                [ZCTool hideLoadingOnKeyWindow];
                if (!isSuccess) {
                    kShowToast(@"Failed to set wifi.");
                    return;
                }
                //设置成功
                SLConnectBridgeVC *vc = [[SLConnectBridgeVC alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }];
        }];
    }
    
}

@end
